package com.solarStruggle.controls

import java.awt.{Color, Font}
import scala.swing.{Alignment, GridPanel, Label}

/**
 * This class is used to display info on a class.  The display is split into key-value pairs.
 */
class InfoDisplay[A]
extends MigPanel("", "[50%][50%]") {
	
	peer.setName("infoDisplay")

	/**
	 * The fields displayed in this info pane
	 */
	object fields {

		/**
		 * Class used internally to store information about the fields
		 */
		private case class FieldInfo(label: Label, value: Label, fn: A => String)

		/**
		 * Information about the fields displayed
		 */
		private var fieldInfos: List[FieldInfo] = Nil

		/**
		 * Add a new field to this display
		 */
		def add(label: String, fn: A => String) {
			val newInfo = FieldInfo(
				new Label() {
					peer.setName("infoDisplay")
					horizontalAlignment = Alignment.Right
					text = label
				}, new Label() {
					peer.setName("infoDisplay")
					horizontalAlignment = Alignment.Left
				}, fn)

			InfoDisplay.this.add(newInfo.label, "growx")
			InfoDisplay.this.add(newInfo.value, "growx, wrap")

			fieldInfos ::= newInfo
		}

		/**
		 * Sets the value for a given field
		 */
		def setData(a: Option[A]) {
			a match {
				case Some(x) =>
					for(field <- fieldInfos) {
						field.value.text = field.fn(x)
					}
				case None =>
					for(field <- fieldInfos) {
						field.value.text = ""
					}
			}
		}
	}

	/**
	 * The piece of data being displayed
	 */
	private var _data: Option[A] = None

	def data: Option[A] = _data

	def data_=(value: Option[A]) {
		_data = value
 
		fields.setData(value)
	}

	def refresh() { fields.setData(data) }
}
